package com.jiexin.common;

import com.jiexin.service.impl.ReadXmlServiceImpl;
import com.jiexin.util.ConfigUtil;
import java.io.File;
import com.jiexin.util.FileUtil;
import com.jiexin.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: Jay_Liu
 * @Description:  File Listener
 * @Date: Created in 21:19 2018/1/19
 * @Modified By:
 */
@Slf4j
@Component
public class FileListener extends FileAlterationListenerAdaptor {

    private static ReadXmlServiceImpl readXmlService;

    @Autowired
    private ConfigDatasource configDatasource;

    /**
     * 监控新增文件
     * @param file 新增的文件对象
     */
    @Override
    public void onFileCreate(File file) {

        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //初始化bean对象
        if (readXmlService == null) {
            readXmlService = SpringUtil.getBean("readXmlServiceImpl", ReadXmlServiceImpl.class);
        }
        log.info("[new] file path is :{}", file.getAbsolutePath() + "," + readXmlService);
        if (file.getName().toUpperCase().endsWith(".AXML")) {
            String currentFileName = file.getName();
            String tempPath = configDatasource.getConfig("tempPath");
            String xmlPath = configDatasource.getConfig("tempPath") + "agile.xml";
            String successFilePath = configDatasource.getConfig("successBackup") + currentFileName;
            String failFilePath = configDatasource.getConfig("failBackup") + currentFileName;
            String currentFilePath =  file.getAbsolutePath();
            log.info("begin to import data from {}  ", currentFilePath);
            try {
                //解压AXML文件
                log.info("begin to extracte AXML files......");
                FileUtil.getXMLFromzip(currentFilePath, tempPath);
                //解析Xml文件
                readXmlService.readFileAndWriteInTable(xmlPath, currentFileName, currentFilePath, successFilePath, failFilePath);
            } catch (Exception e) {
                log.error("Failed to read the file, the failure message is:{}", e);
            }
            log.info("end to import data from {}", currentFilePath);

        }
    }

    /**
     * 修改文件
     * @param file 文件对象
     */
    @Override
    public void onFileChange(File file) {
        log.info("[modify]:" + file.getAbsolutePath());
    }

    /**
     * 删除文件
     * @param file 文件对象
     */
    @Override
    public void onFileDelete(File file) {
        log.info("[delete]:" + file.getAbsolutePath());
    }

    /**
     * 新增文件夹
     * @param directory 文件夹对象
     */
    @Override
    public void onDirectoryCreate(File directory) {
        log.info("[new]:" + directory.getAbsolutePath());
    }

    /**
     * 修改文件夹
     * @param directory 文件夹对象
     */
    @Override
    public void onDirectoryChange(File directory) {
        log.info("[modify]:" + directory.getAbsolutePath());
    }

    /**
     * 删除文件夹
     * @param directory 文件夹对象
     */
    @Override
    public void onDirectoryDelete(File directory) {
        log.info("[delete]:" + directory.getAbsolutePath());
    }

    @Override
    public void onStart(FileAlterationObserver observer) {
        super.onStart(observer);
    }

    @Override
    public void onStop(FileAlterationObserver observer) {
        super.onStop(observer);
    }

}
